#include"SeqList.h"

void SLInit(SL* ps)
{
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

void SLDestroy(SL* ps)
{
	if (ps->arr == NULL)
	{
		free(ps->arr);
	}
	ps->arr = NULL;
	ps->size = ps->capacity = 0;
}

void SLCheckCapacity(SL* ps)
{
	assert(ps);
	int Newcapacity = 0;
	if (ps->size == ps->capacity)
	{
		Newcapacity = (ps->capacity == 0 ? 4 : 2 * ps->capacity);
		SLDataType* tmp = (SLDataType*)realloc(ps->arr, sizeof(SLDataType) * Newcapacity);
		if (tmp == NULL)
		{
			perror("realloc failed!");
			return;
		}
		ps->capacity = Newcapacity;
		ps->arr = tmp;
	}
}
void SLPushBack(SL* ps,SLDataType x)
{
	assert(ps);
	SLCheckCapacity(ps);
	ps->arr[ps->size++] = x;
}

void SLErase(SL* ps,int pos)
{
	assert(ps);
	assert(ps->size);
	for (int i = pos; i < ps->size - pos-1; i++)
	{
		ps->arr[i] = ps->arr[i + 1];
	}
	ps->size--;
}